var areaName;
var onlinePayType = localStorage.getItem("onlinePayType");
$(function(){
	$(".orient span").bind("click",function(){
		$(this).addClass("select1");
		$(this).siblings("span").removeClass("select1");
		var oDiv = $("div");
		oDiv.css("display","none");
		oDiv.eq($(this).index()).css("display","block");
	});
	$(".area_img").bind("click",function(){
		if(!hasOpen(this)){
			initAreaAll();
			closeAll();
			openOne(this);	
		}else{
			closeAll();
		}
	});
	var m = 0;
	$("#all_area").prop("onOff",true);
	$(".area").prop("onOff",true);
	$(".user").prop("onOff",true);
	function select2(){
		//initAreaAll();
		var all_area = document.getElementById("all_area"); 
		var area_list = document.getElementById("area_list")
		var aArea = area_list.getElementsByTagName("span");
		$("#all_area").tap(function(){
			if($(this).prop("onOff") == true){
				this.src = "img/choIcon.png";
				/*var spans = $(this).parent().parent().find('span');
				var s="";
				$(spans).each(function(){
					s += $($(this).find('input')[0]).val()+",";
				});
				areaName= $("#all_area").attr("value",s).attr("value");*/
				//alert(areaName);
				//console.log(s);	
				$(this).prop("onOff",false);
				m = aArea.length;
				if( m == aArea.length ){
					$(".area").attr("src","img/choIcon.png");
					$(".area").prop("onOff",false);
				};
			}else{
				this.src = "img/choIcon1.png";
				$(this).prop("onOff",true);
				m = 0;
				$(".area").attr("src","img/choIcon1.png");
				
				$(".area").prop("onOff",true);
			};
			//$("#area_list span").attr("data","sele1");
		});
		$(".area").live('click',function(){
			if($(this).prop("onOff") == true ||$(this).prop("onOff") == undefined){
				this.src = "img/choIcon.png";
				/*if($("#all_area").attr("value") == null ||$("#all_area").attr("value")=="null"){
					areaName= $("#all_area").attr("value",$(this).siblings("input").attr("value")).attr("value");
				}else{
					areaName = $("#all_area").attr("value",$("#all_area").attr("value")+","+$(this).siblings("input").attr("value")).attr("value");
				}*/
				$(this).prop("onOff",false);
				//$(this).parent().attr("data","sele1");
				m++;
				if( m == aArea.length ){
					all_area.src = "img/choIcon.png";
					$("#all_area").prop("onOff",false);
				};
			}else{
				$(this).prop("onOff",true);
				this.src = "img/choIcon1.png";
				//$(this).parent().attr("data","sele2");
				//$(this).siblings("input").attr("value").remove();
				m--;
				if( m != aArea.length ){
					all_area.src = "img/choIcon1.png";
				};
			};
		});
	};
	select2();
	$("#btn").bind('click',function(){
		submit();
	});
	/*$("#mobile").bind('change', function() {
		vailmobile();
	});*/
});

function closeAll(){
	$(".area_list").css("display","none");
	$("#area_list").find("span").empty();
	$(".area_img").attr("src","img/choIcon1.png");
};
function openOne(im){
	
	$(im).attr("src","img/choIcon.png");
	$(im).siblings().css("display","block");
	
};
function hasOpen(im){
	var b = $(im).next().css("display") =='block';
	return b;
};

function submit() {
	var jsonObj = [];
	//alert($("#skuProductForm").serialize());return;
	//var jsonObj = $("#skuProductForm").serializeArray();
	//alert($(".area")[0].src);
	var s ="";
	$(".area").each(function(){
		if ($(this).attr("src")=="img/choIcon.png") {
			s += $(this).prev().val()+",";
		}
	});
	areaName = s;
	if(areaName != "" ){
		jsonObj.push({name:"specId",value:specId});
		jsonObj.push({name:"catchId",value:catchId});
		jsonObj.push({name:"weightId",value:weightId});
		jsonObj.push({name:"productId",value:productId});
		jsonObj.push({name:"PicturePath1",value:PicturePath1});
		jsonObj.push({name:"PicturePath2",value:PicturePath2});
		jsonObj.push({name:"PicturePath3",value:PicturePath3});
		jsonObj.push({name:"onlinePayType",value:onlinePayType});
		jsonObj.push({name:"price",value:price});
		jsonObj.push({name:"overPlusNum",value:overPlusNum});
		jsonObj.push({name:"remark",value:remark});
		jsonObj.push({name:"areaName",value:areaName});
		jsonObj.push({name:"userid",value:userid});
		jsonObj.push({name:"addUser",value:username});
		//jsonObj.push({name:"buyerId",value:$("#buyerId").val()});
		//alert(remark);return;
		//alert(JSON.stringify(jsonObj));
			$.ajax({
				url:server_url+'/rest/product/skuProductAdd.do?ran='+Math.random(),
				type:'post',
				data:jsonObj,
				dataType:'json',
				contentType:'application/x-www-form-urlencoded; charset=utf-8',
				success:function(data){
					easyAlert("发布商品成功！");
					back();
				}
			});	
	}
	if(areaName == "" && $('#mobile').val() !=""){
			if(vailmobile()){
				jsonObj.push({name:"specId",value:specId});
				jsonObj.push({name:"catchId",value:catchId});
				jsonObj.push({name:"weightId",value:weightId});
				jsonObj.push({name:"productId",value:productId});
				jsonObj.push({name:"PicturePath1",value:PicturePath1});
				jsonObj.push({name:"PicturePath2",value:PicturePath2});
				jsonObj.push({name:"PicturePath3",value:PicturePath3});
				jsonObj.push({name:"price",value:price});
				jsonObj.push({name:"onlinePayType",value:onlinePayType});
				jsonObj.push({name:"overPlusNum",value:overPlusNum});
				jsonObj.push({name:"remark",value:remark});
				jsonObj.push({name:"userid",value:userid});
				jsonObj.push({name:"addUser",value:username});
				jsonObj.push({name:"buyerId",value:$("#buyerId").val()});
				//alert(remark);return;
				//alert(JSON.stringify(jsonObj));
					$.ajax({
						url:server_url+'/rest/product/skuProductAdd.do?ran='+Math.random(),
						type:'post',
						data:jsonObj,
						dataType:'json',
						//contentType:'application/x-www-form-urlencoded; charset=utf-8',
						success:function(data){
							easyAlert("发布商品成功！");
							back();
						}
					});	
		}
	}
	if(areaName == "" && $('#mobile').val() ==""){
		jsonObj.push({name:"specId",value:specId});
		jsonObj.push({name:"catchId",value:catchId});
		jsonObj.push({name:"weightId",value:weightId});
		jsonObj.push({name:"productId",value:productId});
		jsonObj.push({name:"PicturePath1",value:PicturePath1});
		jsonObj.push({name:"PicturePath2",value:PicturePath2});
		jsonObj.push({name:"PicturePath3",value:PicturePath3});
		jsonObj.push({name:"price",value:price});
		jsonObj.push({name:"onlinePayType",value:onlinePayType});
		jsonObj.push({name:"overPlusNum",value:overPlusNum});
		jsonObj.push({name:"remark",value:remark});
		jsonObj.push({name:"userid",value:userid});
		jsonObj.push({name:"addUser",value:username});
		//jsonObj.push({name:"buyerId",value:$("#buyerId").val()});
		//alert(remark);return;
		//alert(JSON.stringify(jsonObj));
			$.ajax({
				url:server_url+'/rest/product/skuProductAdd.do?ran='+Math.random(),
				type:'post',
				data:jsonObj,
				dataType:'json',
				//contentType:'application/x-www-form-urlencoded; charset=utf-8',
				success:function(data){
					localStorage.removeItem("specId");
					localStorage.removeItem("catchId");
					localStorage.removeItem("weightId");
					localStorage.removeItem("productId");
					localStorage.removeItem("PicturePath1");
					localStorage.removeItem("PicturePath2");
					localStorage.removeItem("PicturePath3");
					localStorage.removeItem("price");
					localStorage.removeItem("overPlusNum");
					localStorage.removeItem("remark");
					easyAlert("发布商品成功！");
					back();
				}
			});	
	}
	
		
};
function back() {
	window.location.href="index.do?ran="+Math.random();
};
function initAreaAll() {
	$.ajax({
		url:server_url+'/rest/product/initAreaAll.do',
		type:'get',
		async:false,
		data:{userId:userid},
		success:function(data){
			if(data.retCode=="0000"){
				console.log(data.data);
				//var areaId = [];
				/*for(var i=0;i<data.data.length;i++){
					areaId = data.data[i].areaID ;					
				}*/
				var areaId = data.data.data.areaID;
				console.log(data.data.data.areaID);
				var areaIds = areaId.split(",");
				replaceEmptyItem(areaIds);
				//console.log(areaIds.length);
				//InitData(areaIds,data.data);
				initSupplyAreaAll(areaIds,data.data);
				/*for(var i=0;i<data.data.length;i++){
					if(data.data[i].cityName == '市辖区'){
						$("#area_list").append("<span >"+data.data[i].provinceName+','+data.data[i].provinceName+','+data.data[i].countyName+"<input type='hidden' value='"+data.data[i].areaID+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
					}else{
						$("#area_list").append("<span >"+data.data[i].provinceName+','+data.data[i].cityName+','+data.data[i].countyName+"<input type='hidden' value='"+data.data[i].areaID+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
					}	
				}*/
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					      content : data.retMsg
					  },
					  autoClose : 2000
					});				
			} 			
		}
	});

};
function replaceEmptyItem(arr){
    for(var i=0,len=arr.length;i<len;i++){
        if(!arr[i]|| arr[i]==''){
            arr.splice(i,1);
            len--;
             
        }
    }
};
//初始化大类数据
/*function InitData(areaIds,data){
	console.log(areaIds);
	for (var i = 0; i < data.length; i++) {
		 var areaId=data[i].areaID;
		 if(areaIds.contains(areaId)) continue;
		 	areaIds.push(areaId);
	} 
}; */

//判断是否包含元素
/*Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
};*/
function initSupplyAreaAll(areaIds,data){
	console.log(areaIds);
	console.log(data);
	for(var i =0;i<areaIds.length;i++){
		/*for(var j=1;j<data.length;j++){
			if(data[j].areaID != areaIds[i]) continue;
			//console.log(data[j].areaID != areaIds[i]);return;
			if(data[j].cityName == '市辖区'){
				$("#area_list").append("<span >"+data[j].provinceName+','+data[j].provinceName+','+data[i].countyName+"<input type='hidden' value='"+data[j].areaID+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
			}else{
				$("#area_list").append("<span >"+data[j].provinceName+','+data[j].cityName+','+data[j].countyName+"<input type='hidden' value='"+data[j].areaID+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
			}	
		}*/
		$.ajax({
			url:server_url+'/rest/product/getAreaNameByAreaId.do',
			type:"get",
			data:{areaId:areaIds[i]},
			success:function(datas){
				console.log(datas);
				if(datas.data.data.cityName == '市辖区'){
					$("#area_list").find("em").append("<span >"+datas.data.data.provinceName+' '+datas.data.data.provinceName+' '+datas.data.data.countyName+"<input type='hidden' value='"+datas.data.data.areaId+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
				}else{
					$("#area_list").find("em").append("<span >"+datas.data.data.provinceName+' '+datas.data.data.cityName+' '+datas.data.data.countyName+"<input type='hidden' value='"+datas.data.data.areaId+"' id='areaId' name='AreaID'><img src='img/choIcon1.png' class='area'/></span>");
				}	
			}
		})
	}
};
function vailmobile() {
	var mobile = $('#mobile').val();
	var flag = true;
	if((/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){
		if (checkPhoneIsExist()) {
			easyAlert("该手机号码不存在,请重新输入！");
			flag =  false;
		} else {
			if(mobile == username){
				easyAlert("该商品不能指向自己！");
				return;
			}else{
				selectBuyerId();
			}
			
			flag = true;
		}
	}else{
		easyAlert("请输入正确的手机号格式！")
		return false;
	}
	/*if (checkPhoneIsExist()) {
		easyAlert("该手机号码不存在,请重新输入！");
		flag =  false;
	} else {
		selectBuyerId();
		flag = true;
	}*/
	return flag;
}

function selectBuyerId(){
	$.ajax({
		url:server_url+'/rest/product/selectBuyerId.do?mobile='+$("#mobile").val(),
		type:'get',
		async:false,
		success:function(data){
			if(data.retCode=="0000"){
				console.log(data);
				var buyerId = data.data.buyerId;
				$("#buyerId").val(buyerId);
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					      content : data.retMsg
					  },
					  autoClose : 2000
					});				
			} 			
		}
	});
}
function checkPhoneIsExist() {
	var flag =true;
	$.ajax({
		type : 'post',
		url : server_url + 'rest/frontUser/checkBuyerPhone.do?phone='
				+ $('#mobile').val(),
		dataType : 'json',
		async : false,
		success : function(data) {
			if (data.retCode == "0000") {
				flag = false;
			}
		}
	});
	return flag;
}
$("input[type=file]").change(function(){
	ajaxfileuploadUtil(this,"productPic");
});

function toSkuProductAdd(){
		window.location.href=server_url+"skuProductAdd.do?ran="+Math.random();
}








